﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using Nova_Common;
using System.Data.Common;
using System.Text.RegularExpressions;

public partial class Material_MaterialUpdate : System.Web.UI.Page
{
    DbHelper access = new DbHelper();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    string strPID = "";
    string strMessage = "";
    int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            strPID = Request["PID"].ToString();
        }
        catch (Exception ex)
        {

        }
        if (!IsPostBack)
        {
            Tablebind();
        }
    }

    protected void Tablebind()
    {
        strSQL = "SELECT * FROM TBase_Material WHERE PID = '" + strPID.ToString() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds.Tables[0].Rows.Count > 0)
        {
            txtMat_Code.Text = ds.Tables[0].Rows[0]["Mat_Code"].ToString();
            txtMat_Name.Text = ds.Tables[0].Rows[0]["Mat_Name"].ToString();
            txtS01.Text = ds.Tables[0].Rows[0]["S01"].ToString();
            txtS02.Text = ds.Tables[0].Rows[0]["S02"].ToString();
            txtUnit_Base.Text = ds.Tables[0].Rows[0]["Unit_Base"].ToString();

            txtTM01.Text = ds.Tables[0].Rows[0]["TM01"].ToString();
            txtTM03.Text = ds.Tables[0].Rows[0]["TM03"].ToString();
            txtS04.Text = ds.Tables[0].Rows[0]["S04"].ToString();
            txtMiniNum.Text = ds.Tables[0].Rows[0]["MiniNum"].ToString();
        }

        strSQL = @"SELECT COUNT(1)
                        FROM TBase_Storage TBS 
                        LEFT JOIN dbo.TBase_Interface as TBI ON TBS.SEC_MAT_ID = TBI.SEC_MAT_ID
                        WHERE TBI.MAT_CODE='" + txtMat_Code.Text + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        if (Convert.ToInt32(access.ExecuteScalar(cmd)) > 0)
        {
            txtMat_Code.ReadOnly = true;
        }
    }

    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (!CheckPara(txtMiniNum.Text, out strMessage))
            {
                ShowBox.MessageBox(this.Page, strMessage);
                return;
            }

            strSQL = "UPDATE TBase_Material SET S01='" + txtS01.Text + "',S02='" + txtS02.Text + "',S04='" + txtS04.Text + "',TM01='" + txtTM01.Text + "',TM03='" + txtTM03.Text + "',MiniNum='" + txtMiniNum.Text + "',Mat_Name='" + txtMat_Name.Text + "',Mat_Code='" + txtMat_Code.Text + "',Unit_Base = '" + txtUnit_Base.Text + "' WHERE PID = '" + strPID.ToString() + "'";
            cmd = access.GetSqlStringCommond(strSQL);
            i = access.ExecuteNonQuery(cmd);
            if (i > 0)
            {
                ShowBox.MessageBox(this.Page, "修改成功！");
                ibtnClose_Click(sender, e);
            }
            else
            {
                ShowBox.MessageBox(this.Page, "修改失败！");
            }
        }
        catch (Exception ex)
        {

        }

    }
    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        //this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.returnValue='1';top.close();", true);
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.opener.document.getElementById('hidden').value='1';window.opener.document.getElementById('form1').submit();top.close();", true);
    }

    protected bool CheckPara(string strNum, out string strMessage)
    {
        strMessage = "";
        string word = @"^\d+(\.\d+)?$";
        Match Ma = Regex.Match(strNum, word);
        if (Ma.Success)
        {
            return true;
        }
        else
        {
            strMessage = "最低限值必须正为数字！";
            return false;
        }
    }
}
